/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaPesquisarEntrada.java
 *
 * Created on 10/12/2012, 14:14:23
 */
package gui;

import Fachada.Fachada;
import classes.Entrada;
import classes.Produto;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.DaoException;
import erro.GeralException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author George
 */
public class telaPesquisarEntrada extends javax.swing.JFrame {

    private static Fachada f;

    /** Creates new form telaPesquisarEntrada */
    public telaPesquisarEntrada() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        txtBoxDataEntrada = new javax.swing.JFormattedTextField();
        jButtonPesqEntrada = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TableEntrada = new javax.swing.JTable();
        jButtonDelEntrada = new javax.swing.JButton();
        jButtonListarEntrada = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        LabelNumLote = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        LabelProduto = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("Pesquisar Entrada");

        jLabel2.setText("Data Entrada:");

        try {
            txtBoxDataEntrada.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jButtonPesqEntrada.setText("PESQUISAR");
        jButtonPesqEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesqEntradaActionPerformed(evt);
            }
        });

        TableEntrada.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Cod. Entrada", "Data Entrada", "Produto", "Fornecedor", "Num. Lote", "Quant. Produto", "Observação"
            }
        ));
        TableEntrada.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableEntradaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TableEntrada);

        jButtonDelEntrada.setText("DELETAR");
        jButtonDelEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelEntradaActionPerformed(evt);
            }
        });

        jButtonListarEntrada.setText("LISTAR");
        jButtonListarEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonListarEntradaActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("CANCELAR");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel3.setText("Num. Lote:");

        jLabel4.setText("Produto:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(352, 352, 352)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(jLabel1))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 476, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButtonDelEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonListarEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(202, 202, 202))
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(83, 83, 83)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtBoxDataEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonPesqEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 643, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(LabelNumLote, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(LabelProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LabelProduto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(LabelNumLote, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxDataEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPesqEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonListarEntrada, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                        .addComponent(jButtonDelEntrada, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonListarEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonListarEntradaActionPerformed
        listarTabela();
        txtBoxDataEntrada.setText("");
    }//GEN-LAST:event_jButtonListarEntradaActionPerformed

    private void jButtonPesqEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesqEntradaActionPerformed
        TableEntrada.getColumnModel().getColumn(0).setPreferredWidth(130);
        TableEntrada.getColumnModel().getColumn(1).setPreferredWidth(200);
        TableEntrada.getColumnModel().getColumn(2).setPreferredWidth(300);
        TableEntrada.getColumnModel().getColumn(3).setPreferredWidth(300);
        TableEntrada.getColumnModel().getColumn(4).setPreferredWidth(150);
        TableEntrada.getColumnModel().getColumn(5).setPreferredWidth(200);
        TableEntrada.getColumnModel().getColumn(6).setPreferredWidth(500);
        DefaultTableModel modelo = (DefaultTableModel) TableEntrada.getModel();
        modelo.setNumRows(0);
        f = Fachada.getInstancia();
        Entrada ent = new Entrada();
        ArrayList<Entrada> lista = new ArrayList<Entrada>();
        try {
            ent.setDataEntrada(txtBoxDataEntrada.getText());
            lista = (ArrayList<Entrada>) f.pesquisarEntrada(ent);
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        for (int i = 0; i < lista.size(); i++) {
            modelo.addRow(new Object[]{lista.get(i).getCodEntrada(), lista.get(i).getDataEntrada(),
                        lista.get(i).getCodProduto().getDescProduto(),
                        lista.get(i).getCodFornecedor().getNomeFornecedor(),
                        lista.get(i).getNumLote(), lista.get(i).getQuantprod(),
                        lista.get(i).getObsEntrada()});
        }
        TableEntrada.setModel(modelo);
    }//GEN-LAST:event_jButtonPesqEntradaActionPerformed

    private void jButtonDelEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelEntradaActionPerformed
        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Deletar?", "Deletar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            try {
                f = Fachada.getInstancia();
                Entrada ent = new Entrada();
                ent.setNumLote(LabelNumLote.getText());
                Produto prod = new Produto();
                prod.setDescProduto(LabelProduto.getText());
                try {
                    f.pesquisarProd(prod);
                } catch (GeralException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                } catch (DaoException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
                f.deletarEnt(ent, prod);
            } catch (ConsultaException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConexaoException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (GeralException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        } else {
            repaint();
        }
    }//GEN-LAST:event_jButtonDelEntradaActionPerformed

    private void TableEntradaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableEntradaMouseClicked
        int x = TableEntrada.getSelectedRow();
        int y = TableEntrada.getSelectedColumn();

        //Se o clique for duplo
        if (evt.getClickCount() == 1) {
            if (evt.getButton() == evt.BUTTON1) {//E o botão for o esquerdo

                LabelNumLote.setText((String) TableEntrada.getValueAt(x, 4));
                LabelProduto.setText((String) TableEntrada.getValueAt(x, 2));

            }
        }
    }//GEN-LAST:event_TableEntradaMouseClicked

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        telaPesquisarEntrada tlpesqent = new telaPesquisarEntrada();
        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaPesquisarEntrada.this.setVisible(false);
            tlpesqent.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaPesquisarEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new telaPesquisarEntrada().setVisible(true);
            }
        });
    }

    public void listarTabela() {
        TableEntrada.getColumnModel().getColumn(0).setPreferredWidth(130);
        TableEntrada.getColumnModel().getColumn(1).setPreferredWidth(200);
        TableEntrada.getColumnModel().getColumn(2).setPreferredWidth(300);
        TableEntrada.getColumnModel().getColumn(3).setPreferredWidth(300);
        TableEntrada.getColumnModel().getColumn(4).setPreferredWidth(150);
        TableEntrada.getColumnModel().getColumn(5).setPreferredWidth(200);
        TableEntrada.getColumnModel().getColumn(6).setPreferredWidth(500);
        DefaultTableModel modelo = (DefaultTableModel) TableEntrada.getModel();
        modelo.setNumRows(0);
        f = Fachada.getInstancia();
        Entrada ent = new Entrada();
        ArrayList<Entrada> lista = new ArrayList<Entrada>();
        try {
            lista = (ArrayList<Entrada>) f.listarEnt(ent);
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        for (int i = 0; i < lista.size(); i++) {
            modelo.addRow(new Object[]{lista.get(i).getCodEntrada(), lista.get(i).getDataEntrada(),
                        lista.get(i).getCodProduto().getDescProduto(),
                        lista.get(i).getCodFornecedor().getNomeFornecedor(),
                        lista.get(i).getNumLote(), lista.get(i).getQuantprod(),
                        lista.get(i).getObsEntrada()});
        }
        TableEntrada.setModel(modelo);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelNumLote;
    private javax.swing.JLabel LabelProduto;
    private javax.swing.JTable TableEntrada;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonDelEntrada;
    private javax.swing.JButton jButtonListarEntrada;
    private javax.swing.JButton jButtonPesqEntrada;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JFormattedTextField txtBoxDataEntrada;
    // End of variables declaration//GEN-END:variables
}
